package com.example.administrator.nurse.present;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.widget.ItemInput;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jjobes.SlideDateTimeListener;
import jjobes.SlideDateTimePicker;

/**
 * Created by huhaichao on 2017/1/12.
 */

public class NursingAssessmentActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg1,rg2,rg3,rg4,rg5,rg6,rg7,rg8,rg9,rg10;
    int rg1_mark=0,rg2_mark=0,rg3_mark=0,rg4_mark=0,rg5_mark=0,rg6_mark=0,rg7_mark=0,rg8_mark=0,rg9_mark=0,rg10_mark=0;
    private ItemInput room,bed,name,age,sex,time;
    private TextView sexTv;
    private TextView test_time;




    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if ((rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)<= 40) {
                ((TextView)f(R.id.abilityshow)).setText("重度依赖(≤40分)");
                ((TextView)f(R.id.abilityshow)).setTextColor(Color.RED);
                ((TextView)f(R.id.nusingshow)).setText("特级护理");

            }
            if (((rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)<= 60)&&((rg1_mark+rg2_mark+rg3_mark+
                    rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)>=40)) {
                ((TextView)f(R.id.abilityshow)).setText("中度依赖(41-60分)");
                ((TextView)f(R.id.abilityshow)).setTextColor(Color.YELLOW);
                ((TextView)f(R.id.nusingshow)).setText("一级护理");
            }
            if (((rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)<= 99)&&((rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)>=61)) {
                ((TextView)f(R.id.abilityshow)).setText("轻度依赖(61-99分)");
                ((TextView)f(R.id.abilityshow)).setTextColor(Color.BLUE);
                ((TextView)f(R.id.nusingshow)).setText("二级护理");
            }
            if((rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                    rg8_mark+rg9_mark+rg10_mark)> 99){
                ((TextView)f(R.id.abilityshow)).setText("无需依赖(100分)");
                ((TextView)f(R.id.abilityshow)).setTextColor(Color.GREEN);
                ((TextView)f(R.id.nusingshow)).setText("三级护理");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configCommonHeader(NursingAssessmentActivity.this, "护理评估", 1);
        initView();
    }

    @Override
    protected int setLayotId() {
        return R.layout.nursing_assessment_main;
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }

    private void initView(){
        LinearLayout firstContainer = (LinearLayout) f(R.id.firstContainer);
        LinearLayout secondContainer=(LinearLayout)f(R.id.secondContainer);
        /** 第一组 */
        room = new ItemInput(this,firstContainer).withTitle("病房号:").init();
        room.getInput().setNumberLimit(1);
        room.getInput().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        firstContainer.addView(room.getView());
        addGrayLine(firstContainer);

        bed = new ItemInput(this,firstContainer).withTitle("病床号:").init();
        bed.getInput().setNumberLimit(1);
        room.getInput().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        firstContainer.addView(bed.getView());
        addGrayLine(firstContainer);

        name = new ItemInput(this,firstContainer).withTitle("姓名:").init();
        name.getInput().setNumberLimit(1);
        firstContainer.addView(name.getView());
        addGrayLine(firstContainer);

        age = new ItemInput(this,firstContainer)
                .withTitle("年龄:")
                .init();
        age.getInput().setNumberLimit(1);
        room.getInput().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        firstContainer.addView(age.getView());
        addGrayLine(firstContainer);

        sex = new ItemInput(this,firstContainer)
                .withTitle("性别:")
                .withTvShow(true)
                .init();
        sexTv = sex.getTv();
        firstContainer.addView(sex.getView());
        sexTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectSex();
            }
        });
        addGrayLine(firstContainer);

        /** 测量时间 */
        time = new ItemInput(this,firstContainer)
                .withTitle("测量时间")
                .withTvShow(true);
        test_time = time.getTv();
        firstContainer.addView(time.getView());
        test_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SlideDateTimePicker.Builder(getSupportFragmentManager())
                        .setListener(new SlideDateTimeListener() {
                            @Override
                            public void onDateTimeSet(Date date) {
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                String time = formatter.format(date);
                                /** 把日期存进去 */
                                //test_time.setText(time.substring(0, 4) + "年" + time.substring(5, 7) + "月" + time.substring(8, 10) + "日");
                                test_time.setText(time);
                            }

                        })
                        .setInitialDate(new Date())
                        .setIs24HourTime(true)
                        .build()
                        .show();
            }
        });


        View evaluation1=f(R.id.evaluation1);
        ((TextView)evaluation1.findViewById(R.id.item)).setText("控制大便");
        ((TextView)evaluation1.findViewById(R.id.content1)).setText("失禁");
        ((TextView)evaluation1.findViewById(R.id.content2)).setText("偶尔失禁或需要器具帮忙");
        ((TextView)evaluation1.findViewById(R.id.content3)).setText("能控制；如果需要，能使用灌肠剂或栓剂");
        rg1= ((RadioGroup)evaluation1.findViewById(R.id.radioGroup));
        rg1.setOnCheckedChangeListener(this);

        View evaluation2=f(R.id.evaluation2);
        ((TextView)evaluation2.findViewById(R.id.item)).setText("控制小便");
        ((TextView)evaluation2.findViewById(R.id.content1)).setText("失禁");
        ((TextView)evaluation2.findViewById(R.id.content2)).setText("偶尔失禁或需要器具帮忙");
        ((TextView)evaluation2.findViewById(R.id.content3)).setText("能控制；如果需要，能使用集尿器");
        rg2= ((RadioGroup)evaluation2.findViewById(R.id.radioGroup));
        rg2.setOnCheckedChangeListener(this);

        View evaluation3=f(R.id.evaluation3);
        ((TextView)evaluation3.findViewById(R.id.item)).setText("修饰");
        ((TextView)evaluation3.findViewById(R.id.content1)).setText("需要帮忙");
        ((TextView)evaluation3.findViewById(R.id.content2)).setText("偶尔需要帮忙");
        ((TextView)evaluation3.findViewById(R.id.content3)).setText("独立洗脸、梳头、刷牙、剃须");
        rg3= ((RadioGroup)evaluation3.findViewById(R.id.radioGroup));
        rg3.setOnCheckedChangeListener(this);

        View evaluation4=f(R.id.evaluation4);
        ((TextView)evaluation4.findViewById(R.id.item)).setText("洗澡");
        ((TextView)evaluation4.findViewById(R.id.content1)).setText("需要帮忙");
        ((TextView)evaluation4.findViewById(R.id.content2)).setText("偶尔需要帮忙");
        ((TextView)evaluation4.findViewById(R.id.content3)).setText("独立完成");
        rg4= ((RadioGroup)evaluation4.findViewById(R.id.radioGroup));
        rg4.setOnCheckedChangeListener(this);

        View evaluation5=f(R.id.evaluation5);
        ((TextView)evaluation5.findViewById(R.id.item)).setText("如厕");
        ((TextView)evaluation5.findViewById(R.id.content1)).setText("依赖别人");
        ((TextView)evaluation5.findViewById(R.id.content2)).setText("需要部分帮忙；在穿脱衣裤或使用卫生巾时需要帮助");
        ((TextView)evaluation5.findViewById(R.id.content3)).setText("独立用厕所或便盆，穿脱衣裤，冲洗或清洗便盆");
        rg5= ((RadioGroup)evaluation5.findViewById(R.id.radioGroup));
        rg5.setOnCheckedChangeListener(this);

        View evaluation6=f(R.id.evaluation6);
        ((TextView)evaluation6.findViewById(R.id.item)).setText("进食");
        ((TextView)evaluation6.findViewById(R.id.content1)).setText("依赖别人");
        ((TextView)evaluation6.findViewById(R.id.content2)).setText("需要部分帮助(如切割食物，搅拌食物)");
        ((TextView)evaluation6.findViewById(R.id.content3)).setText("能使用任何需要的装置，在适当的时间内独立进食");
        rg6= ((RadioGroup)evaluation6.findViewById(R.id.radioGroup));
        rg6.setOnCheckedChangeListener(this);

        View evaluation7=f(R.id.evaluation7);
        ((TextView)evaluation7.findViewById(R.id.item)).setText("穿衣");
        ((TextView)evaluation7.findViewById(R.id.content1)).setText("依赖");
        ((TextView)evaluation7.findViewById(R.id.content2)).setText("需要帮助，但在适当的时间内至少完成一半的工作");
        ((TextView)evaluation7.findViewById(R.id.content3)).setText("自理(系、开纽扣、关、开拉锁和穿脱支具)");
        rg7= ((RadioGroup)evaluation7.findViewById(R.id.radioGroup));
        rg7.setOnCheckedChangeListener(this);

        View evaluation8=f(R.id.evaluation8);
        ((TextView)evaluation8.findViewById(R.id.item)).setText("床椅转移");
        ((TextView)evaluation8.findViewById(R.id.content1)).setText("完全依赖别人，不能坐");
        ((TextView)evaluation8.findViewById(R.id.content2)).setText("能坐，但需要帮助或指导才能转移");
        ((TextView)evaluation8.findViewById(R.id.content3)).setText("独立从床到轮椅，再从轮椅到床，包括从床上坐起、刹住轮椅");
        rg8= ((RadioGroup)evaluation8.findViewById(R.id.radioGroup));
        rg8.setOnCheckedChangeListener(this);

        View evaluation9=f(R.id.evaluation9);
        ((TextView)evaluation9.findViewById(R.id.item)).setText("平地行走");
        ((TextView)evaluation9.findViewById(R.id.content1)).setText("不能动");
        ((TextView)evaluation9.findViewById(R.id.content2)).setText("需要1人帮助行走(体力或语言指导)45米");
        ((TextView)evaluation9.findViewById(R.id.content3)).setText("能在水平面上行走45米，可以使用辅助装置，不包括带轮的");
        rg9= ((RadioGroup)evaluation9.findViewById(R.id.radioGroup));
        rg9.setOnCheckedChangeListener(this);

        View evaluation10=f(R.id.evaluation10);
        ((TextView)evaluation10.findViewById(R.id.item)).setText("上下楼梯");
        ((TextView)evaluation10.findViewById(R.id.content1)).setText("不能");
        ((TextView)evaluation10.findViewById(R.id.content2)).setText("需要帮助和监督");
        ((TextView)evaluation10.findViewById(R.id.content3)).setText("独立，可以使用辅助装置");
        rg10= ((RadioGroup)evaluation10.findViewById(R.id.radioGroup));
        rg10.setOnCheckedChangeListener(this);
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio0:
                if (group == rg1)
                    rg1_mark = 0;
                if (group == rg2)
                    rg2_mark = 0;
                if (group == rg3)
                    rg3_mark = 0;
                if (group == rg4)
                    rg4_mark = 0;
                if (group == rg5)
                    rg5_mark = 0;
                if (group == rg6)
                    rg6_mark = 0;
                if (group == rg7)
                    rg7_mark = 0;
                if (group == rg8)
                    rg8_mark = 0;
                if (group == rg9)
                    rg9_mark = 0;
                if (group == rg1)
                    rg10_mark = 0;
               /* Toast.makeText(this,"a="+(rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                        rg8_mark+rg9_mark+rg10_mark),Toast.LENGTH_SHORT).show();*/
                //handler.sendMessage(handler.obtainMessage());
                break;
            case R.id.radio1:

                if (group == rg1)
                    rg1_mark = 5;
                if (group == rg2)
                    rg2_mark = 5;
                if (group == rg3)
                    rg3_mark = 5;
                if (group == rg4)
                    rg4_mark = 5;
                if (group == rg5)
                    rg5_mark = 5;
                if (group == rg6)
                    rg6_mark = 5;
                if (group == rg7)
                    rg7_mark = 5;
                if (group == rg8)
                    rg8_mark = 5;
                if (group == rg9)
                    rg9_mark = 5;
                if (group == rg10)
                    rg10_mark = 5;
               /* Toast.makeText(this,"a="+(rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                        rg8_mark+rg9_mark+rg10_mark),Toast.LENGTH_SHORT).show();
                handler.sendMessage(handler.obtainMessage());*/
                break;
            case R.id.radio2:
                if (group == rg1)
                    rg1_mark = 10;
                if (group == rg2)
                    rg2_mark = 10;
                if (group == rg3)
                    rg3_mark = 10;
                if (group == rg4)
                    rg4_mark = 10;
                if (group == rg5)
                    rg5_mark = 10;
                if (group == rg6)
                    rg6_mark = 10;
                if (group == rg7)
                    rg7_mark = 10;
                if (group == rg8)
                    rg8_mark = 10;
                if (group == rg9)
                    rg9_mark = 10;
                if (group == rg10)
                    rg10_mark = 10;
               /* Toast.makeText(this,"a="+(rg1_mark+rg2_mark+rg3_mark+rg4_mark+rg5_mark+rg6_mark+rg7_mark+
                        rg8_mark+rg9_mark+rg10_mark),Toast.LENGTH_SHORT).show();
                handler.sendMessage(handler.obtainMessage());*/
                break;
        }
        handler.sendMessage(handler.obtainMessage());
    }

    private void addGrayLine(LinearLayout Container){
        View line = LayoutInflater.from(this).inflate(R.layout.gray_line, Container, false);
        Container.addView(line);
    }
    private void selectSex() {
        AlertDialog dlg = new AlertDialog.Builder(this)
                .setTitle("请选择性别")
                .setItems(new CharSequence[]{"男", "女"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 1) {
                            sexTv.setText("女");
                            dialog.cancel();
                        } else {
                            sexTv.setText("男");
                            dialog.cancel();
                        }
                    }
                })
                .create();
        dlg.show();
    }
}
